// import 'package:dgg_customer_flutter/themes/dgg_theme_data.dart';
// import 'package:dgg_customer_flutter/widget/dgg_widget.dart';

import 'package:flutter/material.dart';

typedef TextBuilder<T> = String Function(T);
typedef WidgetBuilder<T> = Widget Function(T);

class SelectDialog<T> extends StatelessWidget {
  final List<T> data;
  final TextBuilder<T> name;
  final WidgetBuilder<T> widget;
  final Color cancelTextColor;
  final Color dividerColor;
  final Function callBackOne;


  SelectDialog(this.data, {this.name, this.widget,this.cancelTextColor,this.dividerColor,this.callBackOne})
      : assert(name != null || widget != null);

  @override
  Widget build(BuildContext context) {
    var childCount = data.length;
    // final colors = GlobalColor.of(context);
    return SafeArea(
      child: Container(
        constraints: BoxConstraints(
          maxHeight: data.length >=8 ? 462.0 : 58.0+data.length*50
        ),
        child: Column(
          children: <Widget>[
            Expanded(
              child: ListView.separated(
              shrinkWrap: true,
              itemBuilder: (context, index) {
                Widget child;
                if (index < data.length) {
                  if (widget != null) {
                    child = widget(data[index]);
                  } else {
                    child = Text(
                      name(data[index]),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(fontSize: 16.0),
                    );
                  }
                } else {
                  // child = Text(
                  //   '取消',
                  //   maxLines: 1,
                  //   overflow: TextOverflow.ellipsis,
                  //   style: TextStyle(fontSize: 16.0, color: cancelTextColor ?? Color(0xFF666666)),
                  // );
                }
                return GestureDetector(
                  onTap: () {
                    bool isEnd = index == childCount - 1;
                    
                    if(callBackOne != null){
                      callBackOne(index);
                    }
                    // Navigator.pop(context, isEnd ? null : data[index]);
                    Navigator.pop(context, data[index]);
                  },
                  child: Container(
                    height: 50.0,
                    color: Colors.transparent,
                    alignment: Alignment.center,
                    padding: EdgeInsets.symmetric(horizontal: 16.0),
                    child: child,
                  ),
                );
              },
              separatorBuilder: (context, index) {
                // bool isEnd = index == childCount - 2;
                bool isEnd = false;
                return Container(
                  color: dividerColor ?? Color(0xFF999999),
                  height: isEnd ? 8 : 0.5,
                  margin: isEnd ? null : EdgeInsets.symmetric(horizontal: 16.0),
                );
              },
              itemCount: childCount),
            ),
            Container(
              height: 8.0,
              color: Color(0xFFEFEFEF),
            ),
            InkWell(
              onTap: (){
                Navigator.pop(context);
              },
              child: Container(
                height: 50.0,
                color: Colors.transparent,
                alignment: Alignment.center,
                padding: EdgeInsets.symmetric(horizontal: 16.0),
                child: Text(
                  '取消',
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(fontSize: 16.0, color: cancelTextColor ?? Color(0xFF666666)),
                )
              ),
            )
          ],
        )
      ),
      top: false,
    );
  }
}

Future<T> showBottomSelectDialog<T>({
  @required BuildContext context,
  @required List<T> data,
  @required Function callBack,
  TextBuilder<T> stringBuilder,
  WidgetBuilder widgetBuilder,
  Color cancelTextColor,
  Color dividerColor,
  double borderRadius = 5,
}) {
  return showModalBottomSheet(
      shape: RoundedRectangleBorder(
        side: BorderSide(color: Theme.of(context).colorScheme.surface),
        borderRadius: BorderRadius.vertical(top: Radius.circular(borderRadius)),
      ),
      context: context,
      isScrollControlled: true,
      builder: (context) {
        return SelectDialog<T>(
          data,
          name: stringBuilder,
          widget: widgetBuilder,
          cancelTextColor: cancelTextColor,
          dividerColor: dividerColor,
          callBackOne: callBack,
        );
      });
}
